package vitamin.demo.certification.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import vitamin.demo.certification.mapper.AdminMapper;
import vitamin.demo.certification.service.AdminService;
import vitamin.demo.certification.service.impl.AdminServiceImpl;
import vitamin.demo.pojo.User;
import vitamin.demo.utils.ResultUtils;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.time.Duration;

/**
 * Admin Controller
 * @Author Hac
 */

@Slf4j
@Controller
@RequestMapping("/api/admin")
@Api(tags = "管理者服务接口")
public class AdminController {

    @Resource
    RedisTemplate redisTemplate;

    private final AdminService adminService;
    private final AdminMapper adminMapper;

    public AdminController(AdminServiceImpl adminService, AdminMapper adminMapper) {
        this.adminService = adminService;
        this.adminMapper = adminMapper;
    }


    /**
     *
     * @param username
     * @param password
     * @return
     */
    @PostMapping("/login")
    @ApiOperation("登录")
    public String login(@RequestParam("username")String username,
                        @RequestParam("password")String password,
                        Model model,HttpSession session){
        try{
            User user=adminService.vertify(adminMapper.getUserByusername(username),password);

            session.setAttribute("loginUser",username);

            String token=adminService.buildToken(username);

            redisTemplate.opsForValue().set(token,user, Duration.ofMinutes(30L));

            return "redirect:/ok.html";
        }catch (Exception e){

            model.addAttribute("msg","用户名或密码不正确");

            return "index";
        }
    }



    @GetMapping("/getLoginInfo")
    public String getLoginInfo(HttpServletRequest request,Model model){
        String token=request.getHeader("token");

        Object user=redisTemplate.opsForValue().get(token);

        if(user!=null){
            return ResultUtils.doSuccess();
        }

        model.addAttribute("getLoginInfoErrorTip","获取登录用户失败，请重新登录");

        return "redirect:/index.html";
    }


    /**
     * 注销
     * @param request
     * @return
     */
    @GetMapping("/logout")
    public String logout(HttpServletRequest request,Model model){
        String token =request.getHeader("token");

        redisTemplate.delete(token);//清除token缓存

        model.addAttribute("logoutTip","注销成功");

        return "redirect:/index.html";
    }



//    @PostMapping("/regester")
//    public String regester(@RequestParam("username") String username,
//                           @RequestParam("password") String password,
//                           @RequestParam("correctPassword") String repeatPassword,
//                           @RequestParam("nickname") String nickname,
//                           Model model){
//        if(password!=repeatPassword){
//            model.addAttribute("regesterTip","两次输入的密码不相同");
//            return "regester";
//        }
//
//        boolean regesterFlag=adminService.register(username,password,nickname);
//
//        return null;
//
//    }



}









